/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alcatelparser.modules;

import alcatelparser.modules.io.FileWriter;
import alcatelparser.tags.ITagParser;
//import java.io.FileWriter;

import alcatelparser.tags.SupplementaryServicesTag;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author geniuz
 */
public class ReducedFormBuilder
    implements IReducedBuilder
{

    public ReducedFormBuilder()
    {
        CONTENT_START_ANCHOR = 34;
        DECODING_CONTENT_START_ANCHOR = 61;
        DECODING_DESC_START_ANCHOR = 35;
        _buffer = new StringBuffer();
        _emptyPrinter = new EmptyTagPrinter(this);
    }

    public final void buildHeader(int ai[], int i, int j)
    {
        if(Env.getEnv().isIncludeEmptyFields())
            _emptyPrinter.release();
        _buffer.insert(0, OutHelper.getRecordNumber(_fWriter.getRecordNumber()));
        try
        {
            _fWriter.write(_buffer.toString());
        }
        catch(IOException ioexception)
        {
            //LOG.error(ioexception);
        }
        _buffer = new StringBuffer();
    }

    public final void writeTagContent(int i, int j, String s, String s1)
    {
        if(Env.getEnv().isIncludeEmptyFields() && !"".equals(s))
            _emptyPrinter.printEmptyTag(s);
        writeEmptyTagContent(i, j, s, s1);
    }

    public final void writeSummary(int i)
    {
        if(_fWriter != null)
        {
            String s = OutHelper.getFooter(_fWriter.getWroteRecordsNumber(), i, 0, 0);
            try
            {
                _fWriter.writeSummary(s);
            }
            catch(IOException ioexception)
            {
               // LOG.error(ioexception);
            }
        }
    }

    public final void writeOverAllHeader(String outpath)
    {
        try {
            _fWriter = new FileWriter(outpath);
        } catch (FileNotFoundException ex) {
            //Logger.getLogger(ReducedFormBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private String getSpaceTillValue(String s, int i)
    {
        String s1 = "";
        if(s == null)
            s = "";
        for(int j = 0; j < i - s.length(); j++)
            s1 = (new StringBuilder()).append(s1).append(I.I(4987)).toString();

        return s1;
    }

    public final void tagParseEnd(ITagParser itagparser)
    {
        if(itagparser instanceof SupplementaryServicesTag)
        {
            SupplementaryServicesTag supplementaryservicestag = (SupplementaryServicesTag)itagparser;
            (new SupplementaryServiceEmptyTagPrinter()).printEmptyTag(supplementaryservicestag.getServiceNumber(), this);
        }
    }

    public final void releaseContent()
    {
        _buffer = new StringBuffer();
        try
        {
            if(_fWriter != null)
                _fWriter.close();
        }
        catch(IOException ioexception)
        {
            //LOG.error(ioexception);
        }
    }

    public final long getWroteSize()
    {
        if(_fWriter == null)
            return 0L;
        else
            return _fWriter.getWroteSize();
    }

    public final int getRecordWroteAmount()
    {
        if(_fWriter == null)
            return 0;
        else
            return _fWriter.getWroteRecordsNumber();
    }

    public final void notifyRecordEnd()
    {
        try {
            _fWriter.notifyRecordEnd();
        } catch (FileNotFoundException ex) {
           // Logger.getLogger(ReducedFormBuilder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
           // Logger.getLogger(ReducedFormBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public final void notifyHeaderEnd(int i, int j, int ai[])
    {
        if(Env.getEnv().isPrintHeader())
            try
            {
                _fWriter.writeOverallHeader((new StringBuilder()).append(I.I(4124)).append(_buffer.toString()).toString());
                _buffer = new StringBuffer();
            }
            catch(IOException ioexception)
            {
               // LOG.error(ioexception);
            }
    }

    public static final String getTypeString(int i)
    {
        switch(i)
        {
        case 5: // '\005'
            return I.I(4064);

        case 4: // '\004'
            return I.I(4069);

        case 6: // '\006'
            return I.I(4074);

        case 7: // '\007'
            return I.I(4090);

        case 8: // '\b'
            return I.I(4110);
        }
        return "";
    }

    public final void writeEmptyTagContent(int i, int j, String s, String s1)
    {
        if(!Env.getEnv().isFieldsDecoding())
        {
            _buffer.append((new StringBuilder()).append(s).append(getSpaceTillValue(s, CONTENT_START_ANCHOR)).append(s1).append(I.I(4062)).toString());
        } else
        {
            String s2 = (new StringBuilder()).append(s).append(getSpaceTillValue(s, DECODING_DESC_START_ANCHOR)).append(getTypeString(j)).toString();
            _buffer.append((new StringBuilder()).append(s2).append(getSpaceTillValue(s2, DECODING_CONTENT_START_ANCHOR)).append(s1).append(I.I(4062)).toString());
        }
    }

    private int CONTENT_START_ANCHOR;
    private int DECODING_CONTENT_START_ANCHOR;
    private int DECODING_DESC_START_ANCHOR;
    private StringBuffer _buffer;
    private EmptyTagPrinter _emptyPrinter;
    private FileWriter _fWriter;
    //private static final Logger LOG = Logger.getLogger(com/alcatel/algo/out/ReducedFormBuilder);

}
